package com.example.lottery_system.service.activitystatus.operator;

import com.example.lottery_system.common.utils.JacksonUtil;
import com.example.lottery_system.dao.DataObject.ActivityPrizeDO;
import com.example.lottery_system.dao.Mapper.ActivityPrizeMapper;
import com.example.lottery_system.service.dto.DrawPrizeStatusDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PrizeOperator extends AbstractActivityOperator{

    @Override
    public Integer sequence() {
        return 1;
    }

    @Autowired
    ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Boolean needConvert(DrawPrizeStatusDto drawPrizeStatusDto) {
        if (null == drawPrizeStatusDto ) {
            log.warn("needConvert::drawPrizeStatusDto 为空！");
            return false;
        }

        if (null == drawPrizeStatusDto.getActivityId() || null == drawPrizeStatusDto.getPrizeId()) {
            log.warn("needConvert::activityId或prizeId为空!");
            return false;
        }

        // 查找活动奖品信息
        ActivityPrizeDO activityPrizeDo = activityPrizeMapper.selectByPrizeId(drawPrizeStatusDto.getActivityId(), drawPrizeStatusDto.getPrizeId());
        if (activityPrizeDo == null) {
            log.warn("needConvert:活动奖品查询失败!");
            return false;
        }


        if (activityPrizeDo.getStatus().equalsIgnoreCase(drawPrizeStatusDto.getTargetPrizeStatus().name())) {
            return false;
        }

        return true;
    }

    @Override
    public Boolean convert(DrawPrizeStatusDto drawPrizeStatusDto) {

        try {
            activityPrizeMapper.updatePrizeStatus(drawPrizeStatusDto.getActivityId()
                    , drawPrizeStatusDto.getPrizeId(), drawPrizeStatusDto.getTargetPrizeStatus().name());
            return true;
        } catch (Exception e) {
            log.warn("{}数据库状态转化失败!", JacksonUtil.writeValueAsString(drawPrizeStatusDto));
            return false;
        }
    }
}
